<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/1/9
 * Time: 16:41
 */

namespace App\Http\Controllers\Setting;


use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Http\Model\AdminModel;
use App\Http\Model\NoticeModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class NoticeController extends Controller
{
    /**
     * 首页公告列表
     * @param Request $request
     * @return mixed
     */
    public function showLists(Request $request){
        $adminId = $request->session()->get('adminId');
        $adminModel = new AdminModel();
        $adminInfo = $adminModel->info(['id' => $adminId]);
        $noticeModel = new NoticeModel();
        $this->data = $noticeModel->showLists(['projectId'=>$adminInfo['projectId']]);

        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 通知详情
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function info(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取通知参数失败',
            'id.integer' => '通知参数类型错误',
        ];
        $param = $request->only('id');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $noticeModel = new NoticeModel();
            $this->data = $noticeModel->info($param);
        } else {
            throw new ApiException($validator->errors()->first(),260101);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 通知列表
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function lists(Request $request)
    {
        $rules = [
            'length' => 'required|integer|in:10,20,50',
            'page' => 'required|integer|min:1',
        ];
        $message = [
            'length.required' => '获取记录条数失败',
            'length.integer' => '记录条数参数类型错误',
            'length.in' => '记录条数参数值不正确',
            'page.required' => '获取起始记录位置失败',
            'page.integer' => '页码参数类型错误',
            'page.min' => '页码参数值不小于:min',
        ];
        $param = $request->only('length','page');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $adminId = $request->session()->get('adminId');
            $adminModel = new AdminModel();
            $adminInfo = $adminModel->info(['id' => $adminId]);
            if ($adminInfo['projectId'] != 1){
                $param['projectId'] = $adminInfo['projectId'];
            }
            $noticeModel = new NoticeModel();
            $lists = $noticeModel->lists($param);
            $countLists = $noticeModel->countLists();
            $this->data = $this->tableFormat($lists,$countLists);
        } else {
            throw new ApiException($validator->errors()->first(),260201);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 添加公告
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function add(Request $request)
    {
        $rules = [
            'projectId' => 'required|integer',
            'title' => 'required',
            'content' => 'required'
        ];
        $message = [
            'projectId.required' => '获取通知查阅项目参数失败',
            'projectId.integer' => '通知查阅项目参数类型错误',
            'title.required' => '请填写通知人题目',
            'content.required' => '请填写通知内容',
        ];
        $param = $request->only('projectId', 'title', 'content');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $noticeModel = new NoticeModel();
            $data = [
                'projectId' => $param['projectId'],
                'title' => $param['title'],
                'content' => $param['content'],
                'createTime' => date('Y-m-d H:i:s'),
                'updateTime' => date('Y-m-d H:i:s'),
                'adminId' => $request->session()->get('adminId'),
            ];
            $noticeModel->insert($data);
        } else {
            throw new ApiException($validator->errors()->first(),260301);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 编辑公告
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function edit(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
            'projectId' => 'required|integer',
            'title' => 'required',
            'content' => 'required'
        ];
        $message = [
            'id.required' => '获取通知参数失败',
            'id.integer' => '通知参数类型错误',
            'projectId.required' => '获取通知查阅项目参数失败',
            'projectId.integer' => '通知查阅项目参数类型错误',
            'title.required' => '请填写通知人题目',
            'content.required' => '请填写通知内容',
        ];
        $param = $request->only(['id','projectId', 'title', 'content']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $noticeModel = new NoticeModel();
            $data = [
                'projectId' => $param['projectId'],
                'title' => $param['title'],
                'content' => $param['content'],
                'updateTime' => date('Y-m-d H:i:s'),
                'adminId' => $request->session()->get('adminId'),
            ];
            $noticeModel->update(['id'=>$param['id']],$data);
        } else {
            throw new ApiException($validator->errors()->first(),260401);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 删除公告
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function delete(Request $request){
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取通知参数失败',
            'id.integer' => '通知参数类型错误',
        ];
        $param = $request->only('id');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $noticeModel = new NoticeModel();
            $noticeModel->delete(['id'=>$param['id']]);
        } else {
            throw new ApiException($validator->errors()->first(),260501);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 公告置顶
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function updateTop(Request $request){
        $rules = [
            'id' => 'required|integer',
            "isTop" => 'required|integer|in:0,1'
        ];
        $message = [
            'id.required' => '获取通知参数失败',
            'id.integer' => '通知参数类型错误',
            'isTop.required' => '获取置顶参数失败',
            'isTop.integer' => '置顶参数类型错误',
            'isTop.in' => '置顶参数值不正确',
        ];
        $param = $request->only(['id','isTop']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $noticeModel = new NoticeModel();
            $noticeModel->update(['id'=>$param['id']],['isTop'=>$param['isTop']]);
        } else {
            throw new ApiException($validator->errors()->first(),260601);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 公告是否显示
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function updateShow(Request $request){
        $rules = [
            'id' => 'required|integer',
            "isShow" => 'required|integer|in:0,1'
        ];
        $message = [
            'id.required' => '获取通知参数失败',
            'id.integer' => '通知参数类型错误',
            'isShow.required' => '获取置顶参数失败',
            'isShow.integer' => '置顶参数类型错误',
            'isShow.in' => '置顶参数值不正确',
        ];
        $param = $request->only(['id','isShow']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $noticeModel = new NoticeModel();
            $noticeModel->update(['id'=>$param['id']],['isShow'=>$param['isShow']]);
        } else {
            throw new ApiException($validator->errors()->first(),260701);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }
}